<%--
	author:daniel.yan
	time:2012/4/30 11:48:58
--%>



<%@page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib uri="TowkerTagLib" prefix="rbac"%>
<%@ taglib uri="/WEB-INF/tags/CommonFunction.tld" prefix="cfn" %> 
<%@include file="/commons/include/html_doctype.html"%>
<html>
<head>
	<title>修改</title>
	<%@include file="/commons/include/upd.jsp" %>
	<script type="text/javascript" src="${ctx}/js/calendar/My97DatePicker/WdatePicker.js"></script>
	<script type="text/javascript" src="${ctx}/js/calendar/date.js"></script>
    <link rel="stylesheet" href="${ctx}/js/jquery/ui/css/jquery-ui.css" />
    <script src="${ctx}/js/jquery/ui/jquery-ui.js"></script>
    <script src="${ctx}/js/jquery/ui/jquery.ui.combobox.js"></script>
	<script type="text/javascript">
$(function(){
	jQuery("#dataForm").validate(
	{
		rules: {
		},
		messages: {
		},
		submitHandler:function(form)
		{
			setGoodsInfo();
			jQuery(form).ajaxSubmit(_options);
		}
	});
	selectDateMonth($("#acc_period_panel"), $("#acc_period"));
	$("#custom_code").combobox();
});
_callback = function() {
	location.href = ctx + "/manage/sell/getConsignment.htm";
}
validCallBack = function()
{
	if($("#goodsInfo").val() == "") {
		alert("请选择产品!");
		return false;
	}
	return true;
};
function setGoodsInfo() {
	var goodsInfoStr = "";
	for(var id in goods) {
		goodsInfoStr += id + ",";
	}
	//alert(goodsInfoStr);
	$("#goodsInfo").attr("value", goodsInfoStr);
}
var goods = {};
//JavaScript Document
function selectGoods() {
	//showModelessDialog
	//encodeURI、encodeURIComponent
	var source = encodeURIComponent(ctx+"/manage/basicData/selectGoods.htm?typeid=&classId=&r=" + Math.random());
	var url = ctx + "/commons/showDialog.jsp?r=" + Math.random() + "&url=" + source;
	var result = window.showModalDialog(url, this, "dialogWidth=600px;dialogHeight=600px;scroll=no");
	var panel = $("#MGoodsPanel");
	var html = "";
	for(var i = 0; i < result.length; i++) {
		var v = result[i];
		if(typeof(goods[v.id]) == "undefined" || goods[v.id] == null) {
			goods[v.id] = v.sn;
			html += '    <tr>'
				+ '      <td>' + v.sn + '<input type="hidden" name="wprice_' + v.id + '" id="wprice_' + v.id + '" value="' + v.wprice + '"  /></td>'
				+ '      <td>' + v.name + '</td>'
				+ '      <td><input name="amount_' + v.id + '" type="text" id="amount_' + v.id + '" value="0" size="10"  onChange="calcGoodsTotalM(this,' + v.id + ',' + v.wprice + ')" /></td>';
			if(1 == 2) {
				html += '<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>';
			} else {
				html += '      <td><input name="discount_' + v.id + '" type="text" id="discount_' + v.id + '" onChange="calcGoodsTotalM(this,' + v.id + ',' + v.wprice + ')" value="100" size="10"  />% </td>'
					+ '      <td><input name="sprice_' + v.id + '" type="text" id="sprice_' + v.id + '" size="10" onChange="calcGoodsTotalM(this,' + v.id + ',' + v.wprice + ')"></td>'
					+ '      <td id="totalM_' + v.id + '"><input name="sumMoney_' + v.id + '" type="text" id="sumMoney_' + v.id + '" size="10" onChange="calcGoodsTotalM(this,' + v.id + ',' + v.wprice + ')"></td>';
			}
				html += '      <td>' + v.weight + '</td>'
				+ '      <td>' + v.unit + '</td>'
				+ '      <td><a href="#" onClick="deleteGoods(this, ' + v.id + ')">删除</a></td>'
				+ '    </tr>';
		}
	}
	panel.append(html);
}
function deleteGoods(el, id) {
	var row = $(el).parent().parent();
	row.remove();
	goods[id] = null;
	delete goods[id];
}
function calcGoodsTotalM(el, id) {	
	var obj = $(el);
	var prefix = obj.attr("name").split('_')[0];
	var sprice = $("#sprice_"+id).val();
	var discount = $("#discount_"+id).val();
	var sumMoney = $("#sumMoney_"+id).val();
	var wprice = $("#wprice_"+id).val();
	var amount = $("#amount_"+id).val();
	if(prefix == "discount" || prefix == "amount") {
		$("#sprice_"+id).attr("value", discount * wprice / 100);
		$("#sumMoney_"+id).attr("value",(discount * wprice / 100 * amount).toFixedU(2));
	} else if(prefix == "sprice") {
		$("#discount_"+id).attr("value", parseInt(sprice / wprice * 100));
		$("#sumMoney_"+id).attr("value", (sprice * amount).toFixedU(2));
	} else {
		$("#discount_"+id).attr("value", parseInt((sumMoney / amount).toFixedU(2) / wprice * 100));
		$("#sprice_"+id).attr("value", (sumMoney / amount).toFixedU(2));
	}
	
}
	</script>
</head>
<body>
<table border="0" cellspacing="0" cellpadding="0" class="listLogo">
	<tr>
		<td class="title">代销单修改</td>
		<td>
			<div class="menuTool">
				<a class="save" id="dataFormSave" href="#">保存</a>
				<a class="back" href="getConsignment.htm">返回</a>
			</div>
		</td>
	</tr>
</table>
<div class="line"></div>
<form id="dataForm" method="post" action="updConsignment2.htm">
<input type="hidden" name="goodsInfo" id="goodsInfo" value="" />
<table border="0" cellspacing="1" cellpadding="0" class="listTable">
	<tr>
		<td class="form_title">供销单号：</td>
		<td class="form_input">${fn:escapeXml(po.consignment_code)}</td>
	
		<td class="form_title">订单号：</td>
		<td class="form_input"><input type="text" id="order_sn" name="order_sn" value="${fn:escapeXml(po.order_sn)}" /></td>
		
		 <td class="form_title">订单日期:</td>
	  <td class="form_input"><input type="text" id="order_date" name="order_date" value="<fmt:formatDate pattern='yyyy-MM-dd' value='${po.order_date}' />" readonly="readonly" class="Wdate" onFocus="WdatePicker({isShowWeek:true})" /></td>
	</tr>
	<tr>
		<td class="form_title">客户代码：</td>
		<td class="form_input"><select name="custom_code" id="custom_code">
          <c:forEach items="${customCodes}" var="d" varStatus="status">
          <option value="${d.custom_code}"<c:if test="${d.custom_code eq po.custom_code}"> selected</c:if>>${d.custom_name}[${d.custom_code}]</option>
          </c:forEach>
      </select></td>
		
		<td class="form_title">&nbsp;</td>
		<td class="form_input">&nbsp;</td>
		
		<td class="form_title">代销日期：</td>
		<td class="form_input"><input type="text" id="consignments_date" name="consignments_date" value="<fmt:formatDate pattern="yyyy-MM-dd" value="${po.consignments_date}" />" readonly="readonly" class="Wdate" onFocus="WdatePicker({isShowWeek:true})" /></td>
	</tr>
	<tr>
	    <td class="form_title">财务统计核算期：</td>
		<td class="form_input"><input type="hidden" id="acc_period" name="acc_period" value="${fn:escapeXml(po.acc_period)}" /><div id="acc_period_panel"></div></td>
		<td class="form_title">销售部门代码：</td>
		<td class="form_input"><select name="dep_code" id="dep_code">
				 <c:forEach items="${departList}" var="d" varStatus="status">
					<option value="${d.dep_code}"<c:if test="${d.dep_code eq po.dep_code}"> selected</c:if>>${d.dep_code}[${d.dep_name}]</option>
				</c:forEach>
	  </select></td>
	
		<td class="form_title">发货的仓库代码：</td>
		<td class="form_input"><select name="depot_code" id="depot_code">
				 <c:forEach items="${deportList}" var="d" varStatus="status">
					<option value="${d.depot_code}"<c:if test="${d.depot_code eq po.depot_code}"> selected</c:if>>${d.depot_code}[${d.depot_nane}]</option>
				</c:forEach>
	  </select></td>
	</tr>
	<tr>
		<td class="form_title">销售线路：</td>
		<td class="form_input"><select name="sell_line" id="sell_line">
        <option value="" >请选择</option>
				 <c:forEach items="${sellLines}" var="d" varStatus="status">
					<option value="${d.line_code}"<c:if test="${po.sell_line eq d.line_code}"> selected</c:if>>${d.line_name}</option>
				</c:forEach>
	  </select></td>
	  
	   <td class="form_title">状态：</td>
	  <td class="form_input"><select name="status" id="status">
	    <option value="1"<c:if test="${d.status == 1}"> selected</c:if>>已经审核</option>
        <option value="2"<c:if test="${d.status == 2}"> selected</c:if>>已经记账</option>
        <option value="3"<c:if test="${d.status == 3}"> selected</c:if>>已经审核并记账</option>
      </select></td>
      <td class="form_title"></td>
		<td class="form_input"></td>
	</tr>
    <tr>
		<td class="form_title">备注：</td>
		<td class="form_input" colspan="5"><textarea name="remark" style="width:99%" rows="5" id="remark"></textarea></td>
	</tr>
    <tr>
	  <td class="form_title">订单产品</td>
	  <td class="form_input" colspan="5">
	  <table width="340" border="0"
						cellpadding="2" cellspacing="1" class="listLogo" id="MGoodsPanel">
						<tr>
							<td>产品代码</td>
							<td>产品名称</td>
							<td>订购数量</td>
							<td>产品价标</td>
							<td>产品单价</td>
							<td>产品金额</td>
							<td>重量</td>
							<td>计量单位</td>
								<td><input type="button" name="button" id="button"
									value="增加产品" onClick="selectGoods()">
								</td>
						</tr>
						<c:forEach items="${refGoods}" var="rg" varStatus="status">
							<tr>
								<td><script>goods[${rg.goods_id}] = '${rg.goods_sn}';</script>${rg.goods_sn}<input
									type="hidden" name="ref_id" value="${rg.id}" /> <input
									type="hidden" name="wprice_${rg.goods_id}"
									id="wprice_${rg.goods_id}" value="${rg.wprice}" />
								</td>
								<td>${rg.goods_name}</td>
								<td>
											<input name="amount_${rg.goods_id}" type="text"
												id="amount_${rg.goods_id}" value="${rg.amount}" size="10" onChange="calcGoodsTotalM(this,${rg.goods_id})" />
										</td>
								<td>
											<input name="discount_${rg.goods_id}" type="text"
												id="discount_${rg.goods_id}"
												onchange="calcGoodsTotalM(this,${rg.goods_id})"
												value="${rg.discount}" size="10" />
										%</td>
								<td id="sprice_td_${rg.goods_id}">
											<input name="sprice_${rg.goods_id}" type="text"
												id="sprice_${rg.goods_id}" size="10" value="${rg.sprice}" onChange="calcGoodsTotalM(this,${rg.goods_id})">
								</td>
								<td id="sumMoney_td_${rg.goods_id}">
											<input name="sumMoney_${rg.goods_id}" type="text"
												id="sumMoney_${rg.goods_id}" size="10"
												value="${rg.sumMoney}" onChange="calcGoodsTotalM(this,${rg.goods_id})">
										
								</td>
								<td>${rg.weight}</td>
								<td>${rg.unit} </td>
									<td><a href="#"
										onClick="deleteGoods(this, ${rg.goods_id})">删除</a></td>
							</tr>
						</c:forEach>
					</table>
      </td>
    </tr>
</table>
<input type="hidden" name="id" value="${po.id}" />
</form>
</body>
</html>
